package leetcode.sort;

import java.util.Arrays;

public class ShellSort {
    public int[] shell_sort(int[] sourceArray) {
        int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

        int len = arr.length;

        for (int step = len / 2; step >= 1; step /= 2) {
            for (int i = step; i < len; i++) {
                int tmp = arr[i];
                int j = i - step;
                while (j >= 0 && arr[j] > tmp) {
                    arr[j + step] = arr[j];
                    j -= step;
                }
                arr[j + step] = tmp;
            }
        }
        return arr;
    }

    public static void main(String[] args) {
        ShellSort shellSort = new ShellSort();
        int[] arr = new int[]{1, 3, 5, 7, 3, 9, 6, 3};
        int[] ret = shellSort.shell_sort(arr);
        for (int i : ret) {
            System.out.print(i + " ");
        }
    }
}
